<template>
  <el-container>
    <el-main>
      <el-form ref="form" :model="staffInfo" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="staffInfo.account"></el-input>
        </el-form-item>

        <el-form-item label="用户名">
        <el-input v-model="staffInfo.username"></el-input>
        </el-form-item>

        <el-form-item label="新密码">
        <el-input v-model="staffInfo.password"></el-input>
        </el-form-item>

        <el-form-item label="确认密码">
        <el-input v-model="staffInfo.repeatPassword"></el-input>
        </el-form-item>


        <el-form-item>
          <el-upload
            class="avatar-uploader"
            :action="upload_url"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :data="params"
          >
            <img v-if="staffInfo.avatar" :src="staffInfo.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
<!--        图像上传-->
        <el-form-item label="性别">
          <el-radio-group v-model="staffInfo.sex">
            <el-radio
              :label="i"
              true-label="1"
              v-for="(v,i) in sex_list"
              :key="i"
              :value="i"
            >
              {{v}}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="staffInfo.phone"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="staffInfo.email"></el-input>
        </el-form-item>

        <el-form-item label="身份证">
          <el-input v-model="staffInfo.id_card"></el-input>
        </el-form-item>

        <el-form-item label="地址">
          <el-input v-model="staffInfo.address"></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="staffInfo.status">
            <el-radio
              :label="i"
              :value="i"
              v-for="(v,i) in state_list"
              :key="i"
            >
              {{v}}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="职位">
          <el-checkbox-group  v-model="staffInfo.roleid" @change="selectPosition">
            <el-checkbox
              :label="postion.id"
              v-for="postion in staffInfo.position_list"
              :key="postion.id"
            >
            {{postion.title}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="部门">
          <el-checkbox-group
            v-model="staffInfo.deptid"
            @change="selectDept"
          >
            <el-checkbox
              :label="dept.id"
              v-for="dept in staffInfo.dept_list"
              :key="dept.id"
            >
              {{dept.title}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即修改</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
  import {mapState} from 'vuex'
  import {editStaffInfo} from '@/apilist/Staff'
export default {
  name: "staff_edit",
  data(){
    return {
      staff_id:0,
      state_list:this.fun.state_list,
      sex_list:this.fun.sex_list.filter(Boolean),
      upload_url:this.fun.app_url+'tool/upload_file',
      params:{dir:'avatar'},
      rules:this.fun.formRule,
    };
  },

  computed: {
      ...mapState("SetUser",["staffInfo"]),
  },
  methods:{
    onSubmit(){
        editStaffInfo(this.staffInfo).then(res=>{
            if(res.state == 'ok'){
                this.$message.success(res.msg)
            }else{
                this.$message.error(res.msg)
            }
        });
    },
    //获取选中的值
    getPositionList(val){
      console.log(val);
    },
    //文件上传
    handleAvatarSuccess(res, file) {
      if(res.state == 'err') this.$message.error(res.msg);
      this.staffInfo.avatar = res.data.file;
    },
    beforeAvatarUpload(file) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      setTimeout(() => {
        loading.close();
      }, 2000);
    },
      //选择职位
      selectPosition(val){
          console.log(val)
          this.$forceUpdate()
      },
      //选择部门
      selectDept(dept){
          console.log(dept)
          this.$forceUpdate()
      }
  },
  mounted() {
      console.log(this.staffInfo)
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
